package com.lanou.dllo.soccernews.fragment.shakefragment;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.SlidingDrawer;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.bean.AmusementBean;
import com.lanou.dllo.soccernews.utils.ShakeListener;
import com.lanou.dllo.soccernews.volley.VolleySingleton;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by dllo on 15/10/28.
 */
public class ShakeFragment2 extends Fragment {
    ShakeListener mShakeListener = null;
    Vibrator mVibrator;
    private RelativeLayout mImgUp;
    private RelativeLayout mImgDn;

    private int randomDatas = 0;
    /////////增加音效
    //创建一个soundPool对象
    private SoundPool soundPool;
    //定义一个HashMap用于存放音频流的ID
    final HashMap musicID = new HashMap();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.test, null);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);



        mVibrator = (Vibrator) getActivity().getApplication().getSystemService(Context.VIBRATOR_SERVICE);

        mImgUp = (RelativeLayout) view.findViewById(R.id.shakeImgUp);
        mImgDn = (RelativeLayout) view.findViewById(R.id.shakeImgDown);

        //初始化soundPool,设置音量大小，
        mShakeListener = new ShakeListener(getActivity());
        soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
        musicID.put(1, soundPool.load(getActivity(), R.raw.shake, 1));


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getListener();
    }


    public void startAnim() {   //定义摇一摇动画动画
        AnimationSet animup = new AnimationSet(true);
        TranslateAnimation mytranslateanimup0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -0.5f);
        mytranslateanimup0.setDuration(1000);
        TranslateAnimation mytranslateanimup1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, +0.5f);
        mytranslateanimup1.setDuration(1000);
        mytranslateanimup1.setStartOffset(1000);
        animup.addAnimation(mytranslateanimup0);
        animup.addAnimation(mytranslateanimup1);
        mImgUp.startAnimation(animup);

        AnimationSet animdn = new AnimationSet(true);
        TranslateAnimation mytranslateanimdn0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, +0.5f);
        mytranslateanimdn0.setDuration(1000);
        TranslateAnimation mytranslateanimdn1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -0.5f);
        mytranslateanimdn1.setDuration(1000);
        mytranslateanimdn1.setStartOffset(1000);
        animdn.addAnimation(mytranslateanimdn0);
        animdn.addAnimation(mytranslateanimdn1);
        mImgDn.startAnimation(animdn);
    }

    public void startVibrato() {        //定义震动
        mVibrator.vibrate(new long[]{500, 200, 500, 200}, 0); //第一个｛｝里面是节奏数组， 第二个参数是重复次数，-1为不重复，非-1俄日从pattern的指定下标开始重复
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mShakeListener != null) {
            mShakeListener.stop();
        }
    }



    private void getListener() {
        mShakeListener.setOnShakeListener(new ShakeListener.OnShakeListener() {
            public void onShake() {
                Log.d("TAGGG", "震动监听开始");
                startAnim();  //开始 摇一摇手掌动画
                mShakeListener.stop();
                Log.d("TAGGG", "开始震动");
                startVibrato(); //开始 震动
                Log.d("TAGGG","开启线程");

                        Log.d("TAGGG","播放声音");

                        soundPool.play((Integer) musicID.get(1), 1000, 1000, 7, 0, 1);
                        getChildFragmentManager().beginTransaction().replace(
                                R.id.replace,new MessageFragment()
                        ).commit();

                       mVibrator.cancel();
                       mShakeListener.start();

            }
        });
    }
}


